< previous page page_147 next page >

Page 147
EXERCISE
Look at an Example of a Local Constant
1. Double-click the Hello command button to view the following code:
Private Sub cmdHello_Click()
Const conHELLO As String = "Hello Visual Basic Programmers"

lblHello.Caption = conHELLO

MsgBox conHELLO
End Sub
analysis.gifInstead of the keyword Dim, you use the keyword Const to declare the constant. You give the constant a name and a data type and then you assign the value to the constant all on the same line. Now whenever you want to use this string in the event procedure, just use the constant conHELLO.

Module-level Constants
Next, lets create a module-level constant called mconPI. In the (General) (Declarations) section of the Constants.frm file, youll find the following declaration:
9f31265bb40b592b1929e18a47ab28e5.gif
Const mconPI As Double = 3.14
You can use this constant in any event procedure in this Constants.frm file.
EXERCISE
Using a Module-level Constant
1. Double-click the PI command button to view the following code:
Private Sub cmdPI_Click()
lblPI.Caption = mconPI
End Sub
analysis.gifA module-level constant allows you to use a constant value instead of a hard-coded value throughout one form. This will make it easier to read, and easier to change if needed.

 
< previous page page_147 next page >

If you like this book, buy it!